<?php 
if (!isset($_SESSION)) {
  session_start();
}
class home extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       protected $configs;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('smarty3','','smarty');
            $this->load->model('admin-planners/setting_model');
            $this->load->model('backend/gallery_model','gallery_model');
            $this->load->model('pushclimb/product_model','product_model');
            $this->load->model('pushclimb/tour_model','tour_model');
            $this->load->model('pushclimb/video_model');
            include APPPATH . 'libraries/defu.php';
            if(!isset($_SESSION["SETTINGS"])){
                $this->Init();
            }
            $this->video_model= new video_model();
            $this->setting_model=new setting_model();
        }
        function Init(){
            $data=$this->setting_model->gets();
            foreach ($data as $v){
                $_SESSION["SETTINGS"][$v->Key]=$v->Value;
            }
        }
        public function commingsoon()
	{
            $this->smarty->display("pushclimbing/pages/01_commingsoon");
            
	}
        function index(){
            if(isset($_SESSION["ProductInHome"])){
                $Data["ProductInHome"]=$_SESSION["ProductInHome"];
            }else{
                $pih=$this->setting_model->getByKey("product-in-home");
                $pih=$pih[0];
                if($pih->Update!=date("Y-m-d")){
                    $productinhome=$this->product_model->getRand();
                    if(isset($productinhome[0])){
                        $params=array(
                            "Value"=>$productinhome[0]->ProductID
                        );
                        $this->setting_model->update($pih->ID,$params);
                    }
                }else
                    $productinhome=$this->product_model->get($pih->Value);
                if(isset($productinhome[0])){
                    $Data["ProductInHome"]=($productinhome[0]);
                    $_SESSION["ProductInHome"]=$Data["ProductInHome"];
                }
            }
            if(isset($_SESSION["TourInHome"])){
                $Data["TourInHome"]=$_SESSION["TourInHome"];
            }else{
                $tih=$this->setting_model->getByKey("tour-in-home");
                $tih=$tih[0];
                if($tih->Update!=date("Y-m-d")){
                    $tourinhome=$this->tour_model->getRand();
                    if(isset($tourinhome[0])){
                        $params=array(
                            "Value"=>$tourinhome[0]->ID
                        );
                        $this->setting_model->update($tih->ID,$params);
                    }
                }else
                    $tourinhome=$this->tour_model->get($tih->Value);
                if(isset($tourinhome[0])){
                    $Data["TourInHome"]=($tourinhome[0]);
                    $_SESSION["TourInHome"]=$Data["TourInHome"];
                    
                }
            }
            
            $tmp=$this->gallery_model->get('AD41F1044CBB7668BA3764D259458259');
            $tmp=$tmp[0];
            $Data["Slider"]=$tmp=$this->gallery_model->getDetails($tmp->_id,$tmp->_amount);
            $Data["videos"]=$this->video_model->gets();
            $ab=$this->setting_model->getByKey("AboutPushClimbing");
            $Data["ab"]=$ab[0];
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->assign('Data', $Data)
                    ->display("pushclimbing/pages/02_home");
        }
        function vote(){
            if(isset($_SESSION["push"]["user"])){
                if(!isset($_SESSION["push"]["like"]))$_SESSION["push"]["like"]=array();
                if(!isset($_SESSION["push"]["dislike"]))$_SESSION["push"]["dislike"]=array();
                if($_POST["Like"]=="like"){
                    if(in_array($_POST["ID"], $_SESSION["push"]["like"])){
                        $code=-1;
                        $msg="You have to like this product.";
                    }else{
                        //echo "like";return;
                        if($this->product_model->like($_POST["ID"])){
                            $_SESSION["push"]["like"][]=$_POST["ID"];
                            $code=1;
                            $msg="success. Thank you for this vote.";
                        }else{
                            $code=-1;
                            $msg="Fail. Please try again.";
                        }
                    }
                    
                }else{
                    if(in_array($_POST["ID"], $_SESSION["push"]["dislike"])){
                        $code=-1;
                        $msg="You have to like this product.";
                    }else{
                        //echo "dlike";return;
                        if($this->product_model->dislike($_POST["ID"])){
                            $_SESSION["push"]["dislike"][]=$_POST["ID"];
                            $code=1;
                            $msg="success. Thank you for this vote.";
                        }else{
                            $code=-1;
                            $msg="Fail. Please try again.";
                        }
                    }
                }
                
                
            }else{
                $code=-1;
                $msg="Please login.";
            }
            echo json_encode(array("code" => $code, "msg" => $msg));
        }
        
}
